package com.tao.plan.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tao.plan.common.BaseThreadLocal;
import com.tao.plan.domain.pojo.PCountPlan;
import com.tao.plan.mapper.PCountPlanMapper;
import com.tao.plan.service.PCountPlanService;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class PCountPlanServiceImpl extends ServiceImpl<PCountPlanMapper, PCountPlan> implements PCountPlanService {
	@Override
	public List<PCountPlan> listByStatus(Integer status) {
		LambdaQueryWrapper<PCountPlan> lqw = new LambdaQueryWrapper<>();
		lqw.eq(PCountPlan::getUid, BaseThreadLocal.getUid()).eq(PCountPlan::getStatus,status).orderByDesc(PCountPlan::getUpdateTime);
		return this.baseMapper.selectList(lqw);
	}

	@Override
	public List<PCountPlan> listByStatus(Integer status, Long userId) {
		LambdaQueryWrapper<PCountPlan> lqw = new LambdaQueryWrapper<>();
		lqw.eq(PCountPlan::getUid, userId).eq(PCountPlan::getStatus,status).orderByDesc(PCountPlan::getUpdateTime);
		return this.baseMapper.selectList(lqw);

	}

	@Override
	public boolean incrCount(Long id) {
		LocalDateTime now = LocalDateTime.now();
		PCountPlan pCountPlan = this.baseMapper.selectById(id);
		LocalDateTime last = pCountPlan.getUpdateTime();
		// 判断 now 和 last 是否在同一天
		boolean isSameDay = now.toLocalDate().isEqual(last.toLocalDate());
		if (!isSameDay){
			pCountPlan.setUpdateTime(now);
			pCountPlan.setCount(pCountPlan.getCount()+1);
			this.baseMapper.updateById(pCountPlan);
		}
		return !isSameDay;
	}
}
